package com.ohuang.apkMerge

import java.io.File

/**
 * 查找两个目录下相同的路径的文件
 */
fun searchIdenticalFile(
    path: String,
    path2: String,
    isDelete: Boolean = false
): MutableList<String> {
    var hashMap = HashMap<String, Int>()
    var list = ArrayList<String>()
    addFileToMap(hashMap, path, "", false)
    addFileToMap(hashMap, path2, "", isDelete, list)
    return list

}

fun addFileToMap(
    map: HashMap<String, Int>,
    rootPath: String,
    path: String,
    isDelete: Boolean,
    data: MutableList<String>? = null
) {
    var filePath = rootPath + path
    var file = File(filePath)
    file.listFiles()?.forEach { file0 ->
        if (file0.isDirectory) {
            addFileToMap(map, rootPath, path + "\\${file0.name}", isDelete)
        } else {
            var key = path + "\\" + file0.name

            if (map.contains(key)) {
                if (isDelete) {
                    file0.delete()
                }
                data?.add(file0.absolutePath)
            } else {
                map.put(key, 1)
            }
        }
    }
}